//
//  MyShopHeadView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopHeadView.h"
#import "ShopModel.h"
#import "ShopSubModel.h"
@interface MyShopHeadView()
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *subTitleL;
@property (weak, nonatomic) IBOutlet UILabel *evaluateL;
@property (weak, nonatomic) IBOutlet UILabel *fansL;
@property (weak, nonatomic) IBOutlet UIImageView *startOneIV;
@property (weak, nonatomic) IBOutlet UIImageView *startTwoIV;
@property (weak, nonatomic) IBOutlet UIImageView *startThreeIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFourIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFiveIV;
@property(nonatomic,assign) int b;
@end
@implementation MyShopHeadView
- (void)awakeFromNib {
     [super awakeFromNib];
    self.view.layer.borderColor  = [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:1].CGColor;
    self.view.layer.borderWidth  = 3;
    [self.evaluateBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
    self.evaluateBtn.layer.masksToBounds = YES;
    self.evaluateBtn.layer.cornerRadius = 3;
    self.evaluateBtn.layer.borderColor  = kLineColor.CGColor;
    self.evaluateBtn.layer.borderWidth  = 1;
    [self.collectBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
    [self.collectBtn setTitleColor:kOrangeBg forState:UIControlStateSelected];
    self.collectBtn.layer.masksToBounds = YES;
    self.collectBtn.layer.cornerRadius = 3;
    self.collectBtn.layer.borderColor  = kOrangeBg.CGColor;
    self.collectBtn.layer.borderWidth  = 1;
    self.collectBtn.selected = YES;
    self.evaluateL.textColor = kOrangeBg;
}
#pragma mark-代理:我的店铺
- (void)setShopModel:(ShopModel *)shopModel
{
    _shopModel = shopModel;
    
    if (shopModel.type == 2) {
        self.titleL.text = [NSString stringWithFormat:@"—  %@官方旗舰店  —",shopModel.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x %@企业提供 x",shopModel.title];
    }else {
        self.titleL.text = [NSString stringWithFormat:@"—  %@的店铺  —",shopModel.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x %@个人提供 x",shopModel.title];
    }
    self.evaluateL.text = [NSString stringWithFormat:@"%.1f分",shopModel.evaluate];
    self.fansL.text = [NSString stringWithFormat:@"店铺粉丝： %.1ld人",(long)shopModel.associationCount];
    [self.iv sd_setImageWithURL:[NSURL URLWithString:shopModel.logo] placeholderImage:[UIImage imageNamed:@"headImg"]];
    int a = 0;
    double c = 0;
    self.b = 0;
    float evaluate = shopModel.evaluate/10*5;
    //向下
    a = floor(evaluate);//3.15
    XLog(@"a = %d",a);
    c = evaluate-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = evaluate;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(evaluate);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    XLog(@"b = %d",self.b);
}
#pragma mark-代理:收藏店铺
- (void)setShopSubModel:(ShopSubModel *)ShopSubModel
{
    _ShopSubModel = ShopSubModel;
    if (ShopSubModel.collectStatus == 1) {
        [self.collectBtn setTitle:@"已收藏" forState:UIControlStateNormal];
        self.collectBtn.selected = YES;
    }else {
        [self.collectBtn setTitle:@"添加收藏" forState:UIControlStateNormal];
        self.collectBtn.selected = NO;
    }
    [self.evaluateBtn setTitle:@"店铺介绍" forState:UIControlStateNormal];
    if (ShopSubModel.type == 2) {
        self.titleL.text = [NSString stringWithFormat:@"—  %@官方旗舰店  —",ShopSubModel.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x 企业提供 x"];
    }else {
        self.titleL.text = [NSString stringWithFormat:@"—  %@的店铺  —",ShopSubModel.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x 个人提供 x"];
    }
    self.evaluateL.text = [NSString stringWithFormat:@"%.1f分",ShopSubModel.evaluate];
    self.fansL.text = [NSString stringWithFormat:@"店铺粉丝： %.1ld人",(long)ShopSubModel.associationCount];
    [self.iv sd_setImageWithURL:[NSURL URLWithString:ShopSubModel.logo] placeholderImage:[UIImage imageNamed:@"headImg"]];
    int a = 0;
    double c = 0;
    self.b = 0;
    float evaluate = ShopSubModel.evaluate/10*5;
    //向下
    a = floor(evaluate);//3.15
    XLog(@"a = %d",a);
    c = evaluate-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = evaluate;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(evaluate);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    XLog(@"b = %d",self.b);
}
- (void)setShopArray:(NSMutableArray *)shopArray
{
    _shopArray = shopArray;
    ShopSubModel *model = shopArray[0];
    
    if (model.type == 2) {
        self.titleL.text = [NSString stringWithFormat:@"%@官方旗舰店",model.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x 企业提供 x"];
    }else {
        self.titleL.text = [NSString stringWithFormat:@"%@的店铺",model.title];
        self.subTitleL.text = [NSString stringWithFormat:@"x 个人提供 x"];
    }
    self.fansL.text = [NSString stringWithFormat:@"店铺粉丝： %ld人",model.fansNumber];
    [self.iv sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageNamed:@"headImg"]];
    int a = 0;
    double c = 0;
    self.b = 0;
    float evaluate = model.evaluate/10*5;
    XLog(@"evaluate:%f",evaluate);
    self.evaluateL.text = [NSString stringWithFormat:@"%.1f分",evaluate];
    //向下
    a = floor(evaluate);//3.15
    XLog(@"a = %d",a);
    c = evaluate-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = evaluate;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(evaluate);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    XLog(@"b = %d",self.b);
    //2.1->2
}
-(void)startCount
{
    if (self.b== 0) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_07"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_07"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b>=1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_07"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=2){
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=3){
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=4){
        self.startFourIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if(self.b >=5){
        self.startFiveIV.image = [UIImage imageNamed:@"icon_03"];
    }
    
}
-(void)startHalfCount
{
    
    if (self.b==1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_05"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b == 2){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_05"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b ==3){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_05"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b ==4){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_05"];
    }
    
}
- (IBAction)evaluateBtn:(UIButton *)sender {
    if ([[sender currentTitle] isEqualToString:@"客户评价"]) {
        if ([self.delegate respondsToSelector:@selector(myShopHeadView:withBtn:)]) {
            [self.delegate myShopHeadView:self withBtn:sender];
        }
    }else {
        if ([self.delegate respondsToSelector:@selector(myShopHeadView:withCollectBtn:withShopSubModel:)]) {
            [self.delegate myShopHeadView:self withCollectBtn:sender withShopSubModel:self.ShopSubModel];
        }
    }
    
}

- (IBAction)collectBtn:(UIButton *)sender {
//    sender.selected = !sender.selected;
     if ([[sender currentTitle] isEqualToString:@"编辑介绍"]) {
         if ([self.delegate respondsToSelector:@selector(myShopHeadView:withBtn:)]) {
             [self.delegate myShopHeadView:self withBtn:sender];
         }
     }else {
         
         if ([self.delegate respondsToSelector:@selector(myShopHeadView:withCollectBtn:withShopSubModel:)]) {
             [self.delegate myShopHeadView:self withCollectBtn:sender withShopSubModel:self.ShopSubModel];
         }
     }
}
@end
